#
#	AVR MAKEFILE
#	TODO: clean all *.o

# X Compiler
CC = avr-gcc
LD = avr-ld
AR = avr-ar rcs
NM = avr-nm
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
REMOVE = rm -f
REMOVEDIR = rm -rf
COPY = cp
SHELL = sh

# MCU
MCU = atmega128

# Frequency
F_CPU = 16000000

# Outfut Format
FORMAT = ihex

# TARGET
TARGET = mz

# Optimization [s, 0, 1, 2, 3]
OPT = 2

# Debugging format
DEBUG = dwarf-2

# C standard level
CSTANDARD = -std=gnu99


# Compiler Option
CFLAGS = -g$(DEBUG)
CFLAGS += $(CDEFS)
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CLFAGS += -fpack-struct
CFLAGS += -fshort-enums
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
#CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
CFLAGS += -Wa,-adhlns=$(@:%.o=%.lst)
CFLAGS += $(patsubst %, -I%,$(EXTRAINCDIRS))
CFLAGS += $(CSTANDARD)


# Link Lib
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt

PRINTF_LIB = 
#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)

SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt

SCANF_LIB = 
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)

MATH_LIB = -lm

# Linker Option
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(EXTMEMOPTS)
LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#LDFLAGS += -T linker_script.x


# SOURCES
SRCDIR = ./src
SRC = $(wildcard $(SRCDIR)/*.c)
ASRC = $(wildcard $(SRCDIR)/*.S)
SRCFILES = $(notdir $(SRC))
ASRCFILES = $(notdir $(ASRC))


EXTRAINCDIRS = "./include"

OBJDIR = ./obj
#OBJ = $(OBJDIR)/$(SRC:.c=.o) $(OBJDIR)/$(ASRC:.S=.o)
OBJ = $(SRCFILES:%.c=$(OBJDIR)/%.o) $(ASRCFILES:%.S=$(OBJDIR)/%.o)
#OBJ = $(OBJDIR)/$(SRCFILES:%.c=%.o)
LST = $(OBJ:.o=.lst)

GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)

#all: elf hex eep lss sym
all: lib


elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss
sym: $(TARGET).sym
LIBNAME=lib$(TARGET).a
lib: $(LIBNAME)


files:
	@echo $(SRC) $(ASRC)
	@echo $(OBJ)
	@echo $(LST)


##
clean:
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).eep
	$(REMOVE) $(TARGET).hex
	$(REMOVE) $(TARGET).lss
	$(REMOVE) $(TARGET).sym
	$(REMOVE) $(LIBNAME)
	$(REMOVE) $(OBJ)
	$(REMOVE) $(LST)
	$(REMOVEDIR) .dep



.SECONDARY : $(TARGET).a
.PRECIOUS : $(OBJ)
%.a: $(OBJ)
	@echo
	@echo LIB $@
	$(AR) $@ $(OBJ)

.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
	@echo
	@echo ELF $@
	$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)

%.hex: %.elf
	@echo
	@echo HEX $@
	$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock $< $@

%.eep: %.elf
	@echo
	@echo EEPROM $@
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0	

%.lss: %.elf
	@echo
	@echo extended listing file $@
	$(OBJDUMP) -h -S -z $< > $@

%.sym: %.elf
	@echo
	@echo symbol talbe $@
	$(NM) -n $< > $@

$(OBJDIR)/%.o : $(SRCDIR)/%.c
	@echo
	@echo compile $@
	$(CC) -c $(ALL_CFLAGS) $< -o $@

$(OBJDIR)/%.o : $(SRCDIR)/%.S
	@echo
	@echo comfile $@
	$(CC) -c $(ALL_ASFLAGS) $< -o $@


# Include dependency
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

